@extends('layouts.admin')
@section('content')
<div class="result_wrap">
    <form action="/admin/imgs" id="art_form" method="post" enctype="multipart/form-data">
    {{ csrf_field() }}
        <table class="add_tab">
            <tbody>              
                <tr>
                    <th>
                        分类名称：
                    </th>
                    <tr>
                        <th><i class="require">*</i>缩略图：</th>
                        <td>
                            <input type="text" name="art_thumb" id="art_thumb">
                            {{--文件上传插件--}}
                            <input id="file_upload" name="file_upload" type="file" multiple="true">

                            <p><img id="img1" alt="上传后显示图片"  style="max-width:350px;max-height:100px;" /></p>

                        </td>
                    </tr>
                </tr>
                
                <tr>
                    <th>
                    </th>
                    <td>
                        <input type="submit" value="提交">
                        <input type="button" class="back" onclick="history.go(-1)" value="返回">
                    </td>
                </tr>
            </tbody>
        </table>
    </form>
    <script type="text/javascript">
        $(function () {
            $("#file_upload").change(function () {
                uploadImage();
            });
        });

        function uploadImage() {
//                            判断是否有选择上传文件
            var imgPath = $("#file_upload").val();
            if (imgPath == "") {
                alert("请选择上传图片！");
                return;
            }
            //判断上传文件的后缀名
            var strExtension = imgPath.substr(imgPath.lastIndexOf('.') + 1);
            if (strExtension != 'jpg' && strExtension != 'gif'
                && strExtension != 'png' && strExtension != 'bmp') {
                alert("请选择图片文件");
                return;
            }
//                            var myform = document.getElementById('art_from');
            var formData = new FormData($('#art_form')[0]);
            {{--formData.append('_token', '{{csrf_token()}}');--}}
                                        {{--console.log(formData);--}}
            $.ajax({
                type: "POST",
                url: "/admin/upload",
                data: formData,
                async: true,
                cache: false,
                contentType: false,
                processData: false,
                success: function(data) {
//                                    console.log(data);
//                                    alert("上传成功");
//                    $('#img1').attr('src','http://ovf9o4wws.bkt.clouddn.com/'+data);
//                    http://project187.oss-cn-shanghai.aliyuncs.com/uploads/201708290957357759.jpg
                   $('#img1').attr('src','/'+data);
                    $('#art_thumb').val(data);

                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    alert("上传失败，请检查网络后重试");
                }
            });
        }

    </script>
</div>
@endsection